"""
Case Type   : ODBC+mysql和opengauss兼容B库对比
Case Name   : ODBC默认参数+TINYINT(n)有效取值范围测试
Create At   : 2024/01/12
Owner       : z30056528
Description :
    1.读取通用SQL脚本
    2.解析sql脚本，生成可执行sql语句
    3.通过odbc连接数据库,执行sql
    4.对mysql和opengauss兼容B库结果进行对比
Expect      :
    1.执行成功
    2.执行成功
    3.执行成功
    4.执行成功，结果一致
"""
import os
import unittest

import pyodbc
from Driver_Common import ComODBC
from openGaussBase.testcase.utils import Logger
from yat.test import Node


class ODBCDriver(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        text = f'-----{os.path.basename(__file__)} start-----'
        self.log.info(text)
        self.comodbc = ComODBC()
        self.mysql_node = Node('mysql')
        self.og_node = Node('default')
        self.sql_path = os.path.join(
            'testcase', 'CompatibilityB', 'DATATYPE', 'TINYINT',
            'CompatibilityB_DataType_Tinyint_Case0001.sql')

    def py_exec_sql(self, sql):
        self.log.info('---------------sql-----------------------')
        self.log.info(sql)
        text = '执行规范化后的sql，对mysql和opengauss兼容B库结果进行对比; ' \
               'expect:执行成功，结果一致'
        self.log.info(f'-----{text}-----')
        mysql_conn_info = ('DSN=MYSQLODBC;' + 'PWD='
                           + self.mysql_node.db_password)
        mysql_conn = pyodbc.connect(mysql_conn_info)

        og_conn_info = 'DSN=OGODBC;' + 'PWD=' + self.og_node.db_password
        og_conn = pyodbc.connect(og_conn_info)
        with og_conn, mysql_conn:
            og_cursor = og_conn.cursor()
            mysql_cursor = mysql_conn.cursor()
            with og_cursor, mysql_cursor:
                og_res = self.comodbc.exec_odbc_sql(sql, og_cursor, 0)
                mysql_res = self.comodbc.exec_odbc_sql(sql, mysql_cursor, 0)
                self.log.info('-----------og res---------')
                self.log.info(og_res)
                self.log.info('-----------mysql res---------')
                self.log.info(mysql_res)
                self.log.info('-----对比mysql和opengauss兼容B库执行结果-----')
                diff = self.comodbc.compare_sql_res(mysql_res, og_res, 'mysql',
                                                    'openGauss')
                return diff

    def tearDown(self):
        self.log.info('----run teardown----')
        self.log.info('-----清理环境-----')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
